<template>
  <div class="History">
    <!-- 顶部标题 -->
    <top-title-bar>
      <template #left>
        <div class="left" @click="$router.go(-1)">
          <span class="iconfont icon-xiangzuo1"></span>
          <div class="title">浏览历史</div>
        </div>
        <div class="right" @click="clearFun">
          <div class="title">清空</div>
        </div>
      </template>
    </top-title-bar>

    <div class="wrap">
      <main v-if="historyList">
        <router-link
          tag="div"
          :to="'/PostDetails?postID=' + item.postID + '&from=浏览历史'"
          class="enter-box"
          v-for="item in historyList"
          :key="item.postID"
        >
          <div class="title text-ellipsis-1">{{ item.postTitle }}</div>

          <div class="text">
            <!-- - 左边 -->
            <div class="left">{{ item.categoryTitle }}</div>

            <!-- - 右边 -->
            <div class="right">{{ item.time | numToDate }}</div>
          </div>
        </router-link>
        <van-empty
          v-show="historyList.length == 0"
          class="custom-image"
          image="./img/empty.png"
          description="去其他地方看看吧!"
        />
      </main>
    </div>
  </div>
</template>

<script>
import TopTitleBar from "../components/TopTitleBar.vue";
export default {
  data() {
    return {
      historyList: [],
    };
  },
  methods: {
    // 数据初始化
    dataInit() {
      this.historyListInit();
    },

    clearFun() {
      localStorage.removeItem("historyList");
      this.historyList = [];
      this.$toast("清除成功");
    },

    // 数据初始化
    async historyListInit() {
      let data = JSON.parse(localStorage.getItem("historyList"));
      if (!data) return console.log("本地无更新");
      else this.historyList = data;
      console.log("取到的历史数据 historyList =>", this.historyList);
    },

    // html代码转成字符
    HtmlToText(html) {
      let text = html;
      text = text.split("<text>").join("");
      text = text.split("</text>").join("");
      text = text.split("<image>").join("");
      text = text.split("</image>").join("");
      return text;
    },

    // 将很长的字符只取第一句
    TextToTitle(text) {
      let title = text;
      let arr = [];
      if (text.indexOf(" ") != -1) arr.push(text.indexOf(" "));
      if (text.indexOf("，") != -1) arr.push(text.indexOf("，"));
      if (text.indexOf(".") != -1) arr.push(text.indexOf("."));
      if (text.indexOf("。") != -1) arr.push(text.indexOf("。"));
      if (text.indexOf("；") != -1) arr.push(text.indexOf("；"));
      if (text.indexOf(";") != -1) arr.push(text.indexOf(";"));

      if (!arr.length) return title;

      arr = arr.sort((a, b) => a - b);
      return text.substring(0, arr[0]);
    },
  },
  created() {
    this.dataInit();
  },
  components: {
    TopTitleBar,
  },
  filters: {
    numToDate(val) {
      if (val > 1000000000) {
        let oldTime = parseInt(val);
        let newTime = new Date().getTime();
        let dTime = newTime - oldTime;
        if (dTime / 1000 < 60) return parseInt(dTime / 1000) + "秒前";
        else if (dTime / 1000 / 60 < 60)
          return parseInt(dTime / 1000 / 60) + "分前";
        else if (dTime / 1000 / 60 / 60 < 24)
          return parseInt(dTime / 1000 / 60 / 60) + "小时前";
        else if (dTime / 1000 / 60 / 60 / 24 < 30)
          return parseInt(dTime / 1000 / 60 / 60 / 24) + "天前";
        else if (dTime / 1000 / 60 / 60 / 24 / 30 < 12)
          return parseInt(dTime / 1000 / 60 / 60 / 24 / 30) + "月前";
        else return parseInt(dTime / 1000 / 60 / 60 / 24 / 30 / 12) + "年前";
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.History {
  min-height: 100%;
  background-color: #fff;
  .TopTitleBar {
    .left {
      display: flex;
      align-items: center;
      .icon-xiangzuo1 {
        font-weight: bold;
        color: #fff;
        font-size: 22px;
      }
    }
    .right {
      .icon-xiaoxi,
      .icon-bianji1 {
        font-weight: bold;
        color: #fff;
        font-size: 22px;
        &.icon-xiaoxi {
          margin-left: 18px;
        }
      }
    }
  }
  .wrap {
    padding-top: 0;

    .enter-box {
      padding: 7px 12px 10px;
      border-bottom: 1px solid #eaeaea;

      .title {
        height: 28px;
        line-height: 28px;
        font-size: 15px;
        color: #323232;
      }

      .text {
        display: flex;
        justify-content: space-between;
        color: #bdbdbd;

        .left {
          height: 25px;
          line-height: 25px;
          font-size: 12px;
        }

        .right {
          height: 25px;
          line-height: 25px;
          font-size: 12px;
          height: 21px;
          line-height: 21px;
          margin-top: 3px;
        }
      }
    }
  }
}
</style>